package com.muye.adapter;

import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.muye.dianli.R;
import com.muye.field.CommentListData;

/**
 * 评论列表适配器
 * 
 * @author TangChao
 *
 */
public class CommentListAdapter extends BaseAdapter implements OnKeyListener
{
	/**
	 * 请求数据线程
	 */
	// private RequestHttpPostThread requestHttpPostThread;
	
	/**
	 * 当前操作的条目
	 */
	// private int location;
	
	/**
	 * 缓存按钮,用于判断当前界面是否有按钮显示
	 */
	// private TextView cacheBtn;
	
	/**
	 * 按下时X轴位置
	 */
	// private float x;
	
	/**
	 * 抬起时X轴位置
	 */
	// private float ux;
	
	/**
	 * 删除按钮显示动画
	 */
	// private Animation animationShow;
	
	/**
	 * 删除按钮消失动画
	 */
	// private Animation animationDisappear;
	
	private List<CommentListData> arrayList;
	
	private Context context;
	
	public CommentListAdapter(List<CommentListData> arrayList, Context context)
	{
		super();
		this.arrayList = arrayList;
		this.context = context;
	}
	
	@Override
	public int getCount()
	{
		if (arrayList != null)
		{
			return arrayList.size();
		}
		return 0;
	}
	
	@Override
	public CommentListData getItem(int position)
	{
		if (arrayList != null)
		{
			return arrayList.get(position);
		}
		return null;
	}
	
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		final ViewHolder viewHolder;
		if (convertView == null)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.comment_list_adapter_layout, null);
			viewHolder = new ViewHolder();
			viewHolder.authorText = (TextView) convertView.findViewById(R.id.comment_list_adapter_author_text_view);
			viewHolder.discussText = (TextView) convertView.findViewById(R.id.comment_list_adapter_discuss_text_view);
			viewHolder.createTimeText = (TextView) convertView.findViewById(R.id.comment_list_adapter_create_time_text_view);
			viewHolder.deleteBtn = (TextView) convertView.findViewById(R.id.comment_list_adapter_delete_btn);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (viewHolder != null)
		{
			// initAnimation();
			CommentListData item = arrayList.get(position);
			viewHolder.authorText.setText("昵称: " + item.author);
			viewHolder.discussText.setText(item.discuss);
			viewHolder.createTimeText.setText("评论日期: " + item.created);
			// viewHolder.deleteBtn.setOnClickListener(new OnClickListener()
			// {
			//
			// @Override
			// public void onClick(View v)
			// {
			// deleteComment(arrayList.get(position).id, arrayList.get(position).type);
			// viewHolder.deleteBtn.setVisibility(View.GONE);
			// location = position;
			// cacheBtn = null;
			// }
			// });
			// convertView.setOnTouchListener(new OnTouchListener()
			// {
			// @Override
			// public boolean onTouch(View v, MotionEvent event)
			// {
			// if (event.getAction() == MotionEvent.ACTION_DOWN)
			// {
			// x = event.getX();
			// if (cacheBtn != null)
			// {
			// if (viewHolder.deleteBtn.getVisibility() == View.VISIBLE)
			// {
			// viewHolder.deleteBtn.setVisibility(View.GONE);
			// viewHolder.deleteBtn.startAnimation(animationDisappear);
			// cacheBtn = null;
			// }
			// }
			// }
			// else if (event.getAction() == MotionEvent.ACTION_UP)
			// {
			// ux = event.getX();
			// if (cacheBtn == null)
			// {
			// if (Math.abs(ux - x) > 10)
			// {
			// if (viewHolder.deleteBtn.getVisibility() == View.VISIBLE)
			// {
			// return false;
			// }
			// viewHolder.deleteBtn.setVisibility(View.VISIBLE);
			// viewHolder.deleteBtn.startAnimation(animationShow);
			// cacheBtn = viewHolder.deleteBtn;
			// }
			// }
			// }
			// return true;
			// }
			// });
		}
		return convertView;
	}
	
	private class ViewHolder
	{
		TextView authorText;
		
		TextView discussText;
		
		TextView createTimeText;
		
		TextView deleteBtn;
	}
	
	// /**
	// * 请求删除评论
	// */
	// private void deleteComment(String id, String type)
	// {
	// C.openPragressDialog(context, this, R.string.in_request_data);
	// ArrayList<KeyValue<String, Object>> paramsList = new ArrayList<KeyValue<String, Object>>();
	// String str = C.APPID + id + type + ConstantPersonalInfo.uid + C.CONSTANT;
	// try
	// {
	// paramsList.add(new KeyValue<String, Object>("appId", new StringBody(C.APPID,
	// Charset.forName("utf-8"))));
	// paramsList.add(new KeyValue<String, Object>("id", new StringBody(id, Charset.forName("utf-8"))));
	// paramsList.add(new KeyValue<String, Object>("type", new StringBody(type, Charset.forName("utf-8"))));
	// paramsList.add(new KeyValue<String, Object>("uid", new StringBody(ConstantPersonalInfo.uid, Charset
	// .forName("utf-8"))));
	// paramsList
	// .add(new KeyValue<String, Object>("sign", new StringBody(MD5.getMD5_32(str),
	// Charset.forName("utf-8"))));
	// }
	// catch (Exception e)
	// {
	// e.printStackTrace();
	// }
	// requestHttpPostThread = new RequestHttpPostThread(deleteCommentListener, U.DELETE_COMMENT, paramsList);
	// requestHttpPostThread.start();
	// }
	//
	// /**
	// * 请求删除评论返回监听
	// */
	// private RequestHttpListener deleteCommentListener = new RequestHttpListener()
	// {
	//
	// @Override
	// public void requestHttp(Message msg)
	// {
	// Bundle bundle = msg.getData();
	// int result = bundle.getInt(HttpConst.ACTION_RESULT, HttpConst.ACTION_FAIL);
	// if (result == HttpConst.ACTION_SUCCESS)
	// {
	// byte[] dataArr = bundle.getByteArray(HttpConst.DATA_ARR);
	// try
	// {
	// String str = new String(dataArr, "utf-8");
	// Log.i(C.TAG, "删除评论结果----->" + str);
	// }
	// catch (Exception e)
	// {
	// e.printStackTrace();
	// }
	// Message message = new Message();
	// message.what = 0;
	// message.arg1 = result;
	// handler.sendMessage(message);
	// }
	// }
	// };
	//
	// /**
	// * 处理数据
	// */
	// private Handler handler = new Handler()
	// {
	//
	// @Override
	// public void handleMessage(Message msg)
	// {
	// super.handleMessage(msg);
	// C.closePragressDialog();
	// switch (msg.what)
	// {
	// case 0:// 删除评论
	// switch (msg.arg1)
	// {
	// case HttpConst.ACTION_SUCCESS:
	// C.showToast(context, "删除成功");
	// arrayList.remove(location);
	// notifyDataSetChanged();
	// break;
	// case HttpConst.ACTION_FAIL:
	// C.showToast(context, "删除失败,请重试");
	// break;
	// default:
	// break;
	// }
	// break;
	// default:
	// break;
	// }
	// }
	//
	// };
	//
	// /**
	// * 初始化动画
	// */
	// private void initAnimation()
	// {
	// animationShow = AnimationUtils.loadAnimation(context,
	// R.anim.comment_list_adapter_delete_btn_show_animation);
	// animationDisappear = AnimationUtils.loadAnimation(context,
	// R.anim.comment_list_adapter_delete_btn_disappear_animation);
	// LinearInterpolator linearInterpolator = new LinearInterpolator();
	// animationShow.setInterpolator(linearInterpolator);
	// animationDisappear.setInterpolator(linearInterpolator);
	// }
	//
	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
	{
		// if (requestHttpPostThread != null)
		// {
		// requestHttpPostThread.interrupt();
		// requestHttpPostThread.setRequestHttpListener(null);
		// }
		return false;
	}
}
